// RequestManager.h
#pragma once
#include <QObject>
#include <QHash>
#include <QTimer>
#include <QMutex>
#include <QSet>
#include "SCStatusTcp.h"

class RequestManager : public QObject {
    Q_OBJECT
public:
    struct RequestContext {
        uint16_t cmd;
        QByteArray data;
        QTimer* timer;
        std::function<void(const QByteArray&)> callback;
    };
    
    explicit RequestManager(QObject *parent = nullptr);
    
    void sendRequest(SCStatusTcp* socket, uint16_t cmd, 
                    const QByteArray &data,
                    std::function<void(const QByteArray&)> callback,
                    int timeoutMs = 3000);
    
private slots:
    void handleTimeout(uint16_t cmd);
    void handleResponse(uint16_t recvCmd, uint16_t recvReqId, const QByteArray &data);
    
private:
    uint16_t m_nextReqId = 1;
    QMutex m_mutex;
    // 改为使用 cmd 作为键值
    QHash<uint16_t, RequestContext> m_pendingRequests;
    QSet<SCStatusTcp*> m_connectedSockets;
};